package com.icbc.open.chengdu.demo.settlement.rechargewithdraw;

import com.csii.pp.icbc.util.MD5Util;
import com.csii.pp.icbc.util.Util;
import com.icbc.api.DefaultIcbcClient;
import com.icbc.api.internal.util.internal.util.fastjson.JSON;
import com.icbc.api.request.SettlementAccountRechargeRequestV1;
import com.icbc.api.response.SettlementAccountRechargeResponseV1;
import com.icbc.open.chengdu.demo.BaseData;
import com.icbc.open.chengdu.demo.utils.GenerateKeyUtils;
import com.icbc.open.chengdu.demo.utils.SecretUtils;

/**
 * 结算账户充值
 *
 * @author xcz
 */
public class SettlementAccountRechargeTest {

    public static void main(String[] args) throws Exception {
        String sm4Key= GenerateKeyUtils.generateSM4Key();//生成SM4密钥
        DefaultIcbcClient client = new DefaultIcbcClient(BaseData.APP_ID, BaseData.MY_RSA_PRIVATE_KEY, BaseData.ICBC_RSA_PUBLIC_KEY);
        SettlementAccountRechargeRequestV1 request = new SettlementAccountRechargeRequestV1();
        SettlementAccountRechargeRequestV1.SettlementAccountRechargeRequestV1Biz bizContent = new SettlementAccountRechargeRequestV1.SettlementAccountRechargeRequestV1Biz();
        request.setServiceUrl(BaseData.ACCOUNT_RECHARGE_URL);//API地址
        bizContent.setCorpNo(BaseData.APP_ID); //合作方机构编号
        bizContent.setTrxAccDate(BaseData.TEST_TRX_ACC_DATE); //合作方交易日期
        bizContent.setTrxAccTime(BaseData.TEST_TRX_ACC_TIME); //合作方交易时间
        bizContent.setCorpDate(BaseData.TEST_CORP_DATE); //合作方工作日期
        bizContent.setCorpSerno(Util.getNonceStr(16)); //合作方交易单号
        bizContent.setOutServiceCode("recharge"); //外部服务代码
        bizContent.setMediumId(SecretUtils.encryptSM4(sm4Key, BaseData.TEST_ACCOUNT_NUMBER_2)); //工行联名卡号
        bizContent.setBindMedium(SecretUtils.encryptSM4(sm4Key, BaseData.TEST_ACCOUNT_NUMBER_1)); //绑定的I类卡号
        bizContent.setCcy(1); //币种
        bizContent.setCashExFlag(0); //钞汇标志
        bizContent.setAmount("30000"); //交易金额 300元
        bizContent.setSummary("DY"); //摘要
        bizContent.setRemarks("DY"); //注释
        bizContent.setNotifyAddr(BaseData.TEST_NOTIFY_ADDR); //通知地址
        bizContent.setSecretKey(SecretUtils.encryptSM2(sm4Key)); //sm4对称密钥
        bizContent.setMediumIdHash(MD5Util.string2MD5(BaseData.TEST_ACCOUNT_NUMBER_2)); //卡号哈希值
        bizContent.setBindMediumHash(MD5Util.string2MD5(BaseData.TEST_ACCOUNT_NUMBER_1)); //绑定介质哈希值
        bizContent.setCorrectionFlag(0);//冲账标志
        // bizContent.setCorrectionDate("2019-04-02");//冲账日期 必须正确,否则会报错
        request.setBizContent(bizContent);
        SettlementAccountRechargeResponseV1 response = client.execute(request);
        System.out.println(JSON.toJSONString(response));
        if (response.isSuccess() && response.getReturnCode() == 0) {
            //成功
        } else {
            //失败
        }

    }
}
